package javase2.day06;

public class Demo10 {
	public static void main(String[] args){
		Table table = new Table();
		Table.Person p1 = table.new Person();
		Table.Person p2 = table.new Person();
		p1.start(); p2.start();
	}
}
class Table{
	int beans=20;
//Object cover = new Object();  同步监视器分地以H地OIUASRHWAFJALKSDJFOIUATGAW；BNGSADGHVGASDHIUWA
	public synchronized int getBean(){
		//synchronized(this){
		//synchronized (cover) {
			if(beans==0){
				throw new RuntimeException("over");
			}
			try {
				Thread.sleep(1000);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			return beans--;
		//}
	}
	class Person extends Thread{
		public void run(){
			while(true){
				System.out.println(getName()+":"+getBean());
				Thread.yield();
			}
		}
	}
}